package com.xhj.bigfactory.bigFactory.GC_JVM;
/**
 * -
 *
 * @author 周沫 @Date 2021-08-01 @Time 3:30
 */
public class GCRootsDemo {
  public static void main(String[] args) {
    Data data = new Data();

    Data data1 = data; // java栈中引用的对象:data1

    Data data2 = new Data(1, data1); // 方法区中类静态属性引用的对象：data1

    DataInt dataInt = new DataInt(3);
    data2.i = dataInt.i; // 方法区中常量引用的对象

    System.out.println("==========================================");

    byte[] bytes = new byte[1024 * 1024 * 1024];
    Data data55 = new Data();

    System.gc();

    System.out.println("data55 = " + data55);
    System.out.println("bytes = " + bytes);

    System.out.println("..............");

    System.out.println("data = " + data);
    System.out.println("data1 = " + data1);
    System.out.println("dataInt = " + dataInt);
    System.out.println("data2 = " + data2);

    System.out.println("==========================================");
    System.gc();

    System.out.println("data55 = " + data55);
    System.out.println("bytes = " + bytes);

    System.out.println("..............");

    System.out.println("data = " + data);
    System.out.println("data1 = " + data1);
    System.out.println("dataInt = " + dataInt);
    System.out.println("data2 = " + data2);
  }
}

@lombok.Data
class Data {
  int i;
  static Data data;

  public Data() {}

  public Data(int i, Data data) {
    this.i = i;
    this.data = data;
  }
}

class DataInt {
  int i;

  public DataInt(int i) {
    this.i = i;
  }
}
